package duaxe.home.entity.mob;

import duaxe.home.Game;
import duaxe.home.Graphics.Screen;
import duaxe.home.Graphics.Sprite;
import duaxe.home.entity.projectile.Projectile;
import duaxe.home.entity.projectile.WizardProjectile;
import duaxe.home.input.Keyboard;
import duaxe.home.input.Mouse;

public class Player extends Mob {

	private Keyboard input;
	private Sprite sprite;
	private int anim = 0;
	private boolean walking = false;
	private int fireRate =0;
	

	public Player(Keyboard input) {
		this.input = input;
		sprite = Sprite.player_forward;

	}

	public Player(int x, int y, Keyboard input) {
		this.x = x;
		this.y = y;
		this.input = input;
		sprite = Sprite.player_forward;
		fireRate = WizardProjectile.File_Rate;

	}

	public void update() {
		if(fireRate >0) fireRate--;
		int xa = 0; int ya = 0;
		if (anim < 7500)
			anim++;
		else
			anim = 0;
		if (input.up) {
			ya--;
		}
		if (input.down) {
			ya++;
		}
		if (input.left) {
			xa--;
		}
		if (input.right) {
			xa++;
		}
//		if(Mouse.getButton() ==1){
//			shoot(x,y,dir);
//		}

		if (xa != 0 || ya != 0) {
			move(xa, ya);
			walking = true;
		} else {
			walking = false;
		}
		clear();
		updateShooting();
	}

	private void clear() {
		for(int i=0; i<level.getProjectiles().size(); i++){
			Projectile p = level.getProjectiles().get(i);
			if(p.isRemove())
				level.getProjectiles().remove(i);
		}
	}

	private void updateShooting() {
		if(Mouse.getButton() ==1&&fireRate<=0){
			double dx = Mouse.getX() -Game.getWindowWidth()/2;
			double dy = Mouse.getY() -Game.getWindowHeight()/2;
			double dir = Math.atan2(dy, dx);
			shoot(x,y,dir);
			fireRate = WizardProjectile.File_Rate;
		}
	}

	public void render(Screen screen) {
		int flip = 0;
		if (dir == 0) {
			sprite = Sprite.player_forward;

			if (walking) {
				if (anim % 20 > 10) {
					sprite = Sprite.player_forward_1;
				} else {
					sprite = Sprite.player_forward_2;
				}
			}
		}

		if (dir == 1) {
			sprite = Sprite.player_side;
			if (walking) {
				if (anim % 20 > 10) {
					sprite = Sprite.player_side_1;
				} else {
					sprite = Sprite.player_side_2;
				}
			}
		}
		if (dir == 2) {
			sprite = Sprite.player_back;
			if (walking) {
				if (anim % 20 > 10) {
					sprite = Sprite.player_back_1;
				} else {
					sprite = Sprite.player_back_2;
				}
			}
		}
		if (dir == 3) {
			sprite = Sprite.player_side;
			if (walking) {
				if (anim % 20 > 10) {
					sprite = Sprite.player_side_1;
				} else {
					sprite = Sprite.player_side_2;
				}
			}
			flip = 1;
		}

		// if(dir ==1) sprite = Sprite.player_left;
		// int xx = x-16;
		// int yy = y-16;
		// sprite = Sprite.player_forward;
		screen.renderPlayer(x - 16, y - 16, sprite, flip);

		// screen.renderPlayer(xx+16, yy, sprite.player1);
		// screen.renderPlayer(xx, yy+16, sprite.player2);
		// screen.renderPlayer(xx+16, yy+16, sprite.player3);

	}
}
